
<template>
  <a-card :bordered="false" :bodyStyle="{ padding: '16px', height: '100%' }">
    <div class="table-page-search-wrapper" style="padding-left:10px;padding-right:10px;">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="4" :sm="24">
            <a-form-item label="搜索套餐">
              <a-input v-model="queryParam.keywords" placeholder="请输入套餐名称" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item label="启用状态">
              <a-radio-group v-model="queryParam.project_type">
                <a-radio :value="1">启用</a-radio>
                <a-radio :value="2">停用</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
            >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">清空搜索条件</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" style="margin-right:20px;">新建套餐</a-button>
    </div>
    <s-table
      :columns="columns"
      :data="loadData"
      class="user_management_table"
      style="margin-top:20px;"
      :rowKey="(record) => record.id"
      ref="table"
    >
      <span slot="project_type" slot-scope="text,record">
        <a-switch :checked="text==1?true:false" @click="changeType(text,record)" />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="del(record)">删除</a>
        </template>
      </span>
    </s-table>
    <create-form ref="createModal" @ok="handleOk" />
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import CreateForm from '../../list/modules/CreateForm'
import { comboList, changeComboType, comboDel } from '@/api/courseCharge'
export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm
    // StepByStepModal
  },
  data() {
    return {
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '套餐名称',
          dataIndex: 'project_name',
          width: 150,
          fixed: 'left',
          ellipsis: true,
          align: 'center'
        },

        {
          title: '课程数量',
          dataIndex: 'sum_course',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '物品数量',
          dataIndex: 'sum_goods',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '费用数量',
          dataIndex: 'sum_cost',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '套餐单价',
          dataIndex: 'unit_price',
          ellipsis: true,
          align: 'center',
          customRender(unit_price) {
            return '￥' + unit_price
          }
        },
        {
          title: '启用状态',
          dataIndex: 'project_type',
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'project_type' }
        },

        {
          title: '操作',
          //   width: '100px',
          dataIndex: 'action',
          fixed: 'right',
          align: 'right',
          ellipsis: true,
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return comboList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      }
    }
  },
  created() {},
  methods: {
    changeType(data, e) {
      if (data == 2 || data == null) {
        changeComboType({ project_type: 1 }, e.id)
          .then(res => {
            this.$refs.table.refresh(false)
          })
          .catch(err => {
            this.$message.error(err.data.message||'更新状态失败')
          })
      } else {
        changeComboType({ project_type: 2 }, e.id)
          .then(res => {
            this.$refs.table.refresh(false)
          })
          .catch(err => {
            this.$message.error(err.data.message||'更新状态失败')
          })
      }
    },
    del(e) {
      comboDel(e.id)
        .then(res => {
          this.$message.success('删除成功')
          this.$refs.table.refresh(false)
        })
        .catch(this.$message.error(err.data.message||'删除失败'))
    },
    handleEdit(record) {
    },
    handleSub(record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
<style lang="less" scoped>
.expand_warning {
  margin: 16px 0;
  background-color: #fff0e7;
  border-radius: 4px;
}
</style>